package com.qiku.controller;


import com.github.pagehelper.PageInfo;
import com.qiku.domin.ResponseResult;
import com.qiku.domin.Role;
import com.qiku.domin.User;
import com.qiku.domin.UserVO;
import com.qiku.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVO userVO){
        PageInfo<User> allUserByPage = userService.findAllUserByPage(userVO);
        ResponseResult result = new ResponseResult(true, 200, "相应成功", allUserByPage);
        return  result;
    }


    @GetMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id , String status){
        userService.updateUserStatus(id , status);
        Map<String , String> map = new HashMap<>();
        map.put("status" , status);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;

    }


    @PostMapping("/login")
    public ResponseResult login(UserVO userVO , HttpServletRequest req){
        try {
            User login = userService.login(userVO);
            ResponseResult result = null;
            if (login != null){
                // 登录成功
                Map<String , Object> map = new HashMap<>();
                // 保存 access_token
                String access_token = UUID.randomUUID().toString();
                map.put("access_token" , access_token);
                map.put("user_id" , login.getId());
                // 获取session对象
                HttpSession session = req.getSession();
                // 保存用户的登录状态
                session.setAttribute("user_id" , login.getId());
                session.setAttribute("access_token" , access_token);
                result = new ResponseResult(true, 1, "响应成功", map);
                return result;
            }else {
                // 密码错误
                result = new ResponseResult(false, 201, "登录失败", null);
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 查询当前用户对应的角色信息
     * @return
     */
    @GetMapping("/findUserRoleById")
    public ResponseResult findUserRelationRoleById(Integer id){
        List<Role> roleList = userService.findUserRelationRoleById(id);
        ResponseResult result = null;
        if (roleList != null){
            result = new ResponseResult(true , 200 , "角色回显成功" , roleList);
        }
        return result;

    }


    /**
     * 分配角色
     * @return
     */
    @PostMapping("/userContextRole")
    public ResponseResult userContentRole(@RequestBody UserVO userVO){
        userService.userContentRole(userVO);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", "");
        return result;

    }


    // 获取用户权限
    @GetMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest req){
        // 获取请求头中的 token
        String token = req.getHeader("Authorization");
        // 获取session中的 access_token
        String access_token = (String) req.getSession().getAttribute("access_token");
        // 判断
        if (token.equals(access_token)){
            // 登录成功 从dession获取user_id
            Integer user_id = (Integer) req.getSession().getAttribute("user_id");
            ResponseResult result = userService.getUserPermissions(user_id);
            return result;
        }else {
            return new ResponseResult(false , 201 , "获取失败" , null);
        }


    }



}
